package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Subject;
import cn.wolfcode.crm.query.SubjectQueryObject;
import cn.wolfcode.crm.service.ISubjectService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("subject")
public class SubjectController {

    @Autowired
    private ISubjectService subjectService;

    @RequestMapping("")
    @RequiresPermissions(value = {"科目首页","subject:index"} ,logical = Logical.OR)
    public String index(){

        return "subject/list";
    }

    @RequestMapping("list")
    @RequiresPermissions(value = {"科目列表","subject:list"} ,logical = Logical.OR)
    @ResponseBody
    public PageResult list(SubjectQueryObject qo){
        PageResult result = subjectService.query(qo);
        return result;
    }

    @RequestMapping("save")
    @RequiresPermissions(value = {"科目保存","subject:save"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult save(Subject entity){
        JSONResult result = new JSONResult();
        try{
            subjectService.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"科目更新","subject:update"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult update(Subject entity){
        JSONResult result = new JSONResult();
        try{
            subjectService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }


    @RequestMapping("delete")
    @RequiresPermissions(value = {"科目删除","subject:delete"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            subjectService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(SubjectQueryObject qo) {

        PageResult result = subjectService.query(qo);

        return result;
    }

    @RequestMapping("listData")
    @ResponseBody
    public Object listData(){

        return subjectService.list();
    }



}
